import { _decorator, Component, Node } from 'cc';
import { VIEW_ANIM_EVENT, ViewAnimCtrl } from './ViewAnimCtrl';
const { ccclass, property, requireComponent, menu } = _decorator;

@ccclass('ViewTouchClose')
@menu('ViewAnim/ViewTouchClose')
@requireComponent( ViewAnimCtrl )
export default class ViewTouchClose extends Component {
   
    @property( Node )
    background: Node = null;

    private closeEnable: boolean = false;

    protected onLoad(): void {
        this.background = this.background || this.node;
    }

    protected onEnable(): void {
        this.closeEnable = false;

        this.node.once( VIEW_ANIM_EVENT.InDone, this.onAnimDone,    this);
        this.background.on( Node.EventType.TOUCH_END, this.onTouchEnd,    this);
    }

    protected onDisable(): void {
        this.node.targetOff( this );
    }

    
    private onAnimDone() {
        this.closeEnable = true;
    }


    private onTouchEnd() {
        if( this.closeEnable) {
            this.closeEnable = false;
            this.node.emit( VIEW_ANIM_EVENT.Close );
        }
    }
}


